include apps/Makefile.variable

apps_mod := apps_modules
apps     := $(shell cat $(apps_mod)|grep -v ^\#|awk -F/ '{print $$1 "/" $$2 "/" $$2}')
apps_r   := $(shell cat $(apps_mod)|grep -v ^\#|tac|awk -F/ '{print $$1 "/" $$2 "/" $$2}')
#apps_r   := $(shell cat $(apps_mod)|grep -v ^\#|sed '1!G;h;$$!d'|awk -F/ '{print $$1 "/" $$2 "/" $$2}')
apps_tgz := $(addsuffix .tgz, $(apps))

all: initramfs kernel initramfs.cpio

kernel:
	make -C apps/kernel bzImage

PHONY += initramfs
initramfs: $(apps_tgz) FORCE
	# for dev.tgz, need use sudo
	-sudo rm -rf $@
	mkdir $@
	@cd initramfs; \
		for i in $(apps); do \
			if [ -f $(BASE_DIR)/$$i.tgz ]; then \
	 			sudo tar pzxf $(BASE_DIR)/$$i.tgz; \
			else \
				read -p "!!! NO FOUND $(BASE_DIR)/$$i.tgz !!!"; \
			fi; \
	 	done

PHONY += initramfs.cpio
initramfs.cpio: initramfs
	@echo -e "dev d 0 0\ndev/console c 5 1"|$(BASE_DIR)/tools/mknod_cpio.pl > $@
	@cd initramfs; find .|cpio -R 0:0 -H newc -o -A -F ../$@
	@gzip -f $@

%.tgz: FORCE
ifdef CLEAN_ALL
	$(MAKE) -C `dirname $@` clean
endif
	$(MAKE) -C `dirname $@`; if [ $$? != 0 ]; then echo "!!! MAKE $@ ERROR !!!" > error.log && exit 1; fi

PHONY += clean
clean: FORCE
	@for i in $(apps_r); do \
		$(MAKE) -C `dirname $$i` clean; \
	done
	-rm -rf error.log initramfs initramfs.cpio*

PHONY += FORCE
FORCE:

.PHONY : $(PHONY)

QEMU_KERNEL := $(BASE_DIR)/apps/kernel/linux-$(KERNEL_VER)/arch/i386/boot/bzImage
QEMU_MEM    := 386M
QEMU_NIC0   := vlan=1,macaddr=00:AA:BB:CC:DD:01,model=e1000
QEMU_TAP0   := vlan=1,ifname=tap0,script=no,downscript=no
QEMU_NIC2   := macaddr=00:AA:BB:CC:DD:02,model=e1000
QEMU_NIC3   := macaddr=00:AA:BB:CC:DD:03,model=e1000
QEMU_SRV    := listen=localhost:12345
QEMU_CLI    := connect=localhost:12345
QEMU_HD     := file=hd.img,index=0,media=disk,if=ide,cache=writeback
QEMU_MCAST  := mcast=230.0.0.1:1234


hd.img:
	qemu-img create -f qcow2 $@ 1G

qemu_kernel:
	qemu -m $(QEMU_MEM) -kernel $(QEMU_KERNEL) -hda /dev/zero -net nic,$(QEMU_NIC0) -net tap,$(QEMU_TAP0)

qemu_initramfs: hd.img
	qemu -m $(QEMU_MEM) -kernel $(QEMU_KERNEL) -initrd $(BASE_DIR)/initramfs.cpio.gz -drive $(QEMU_HD) -net nic,$(QEMU_NIC0) -net tap,$(QEMU_TAP0) -net nic,$(QEMU_NIC2) -net socket,$(QEMU_SRV)

qemu_srv: hd.img
	qemu -m $(QEMU_MEM) -kernel $(QEMU_KERNEL) -initrd $(BASE_DIR)/initramfs.cpio.gz -drive $(QEMU_HD) nic,$(QEMU_NIC2) -net socket,$(QEMU_SRV)

qemu_cli:
	qemu -m $(QEMU_MEM) -kernel $(QEMU_KERNEL) -initrd $(BASE_DIR)/initramfs.cpio.gz -hda /dev/zero -net nic,$(QEMU_NIC3) -net socket,$(QEMU_CLI)

qemu_nfs:
	qemu -m $(QEMU_MEM) -kernel $(QEMU_KERNEL) -hda /dev/zero -net nic,$(QEMU_NIC0) -net tap,$(QEMU_TAP0) -append "noinitrd root=/dev/nfs rw nfsroot=193.168.20.133:/nfsroot ip=193.168.20.131::193.168.20.2:255.255.255.0:dragonet:eth0:off init=/linuxrc"

qemu_kgdb: hd.img
	qemu -m $(QEMU_MEM) -kernel $(QEMU_KERNEL) -initrd $(BASE_DIR)/initramfs.cpio.gz -drive $(QEMU_HD) -net nic,$(QEMU_NIC0) -net tap,$(QEMU_TAP0) -serial "pty" -append "console=tty0 kgdboc=ttyS0,9600 kgdbwait"

